package servlet;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import SqlJavaBean.User;

import java.io.PrintWriter;
import java.sql.Connection;

import SqlJavaBean.DBBean;
import SqlJavaBean.StudentR;
/**
 * Servlet implementation class loginServlet
 */
@WebServlet("/loginServlet.view")
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String user = request.getParameter("user");
		String passwd = request.getParameter("passwd");
		User u = new User();
		Connection con = u.userLogin(user, passwd);
		String android = request.getParameter("android");
		PrintWriter out = response.getWriter();
		
		if(android!=null && con!=null){
			out.print(user);
			return;
		}
		if(android!=null && con==null){
			out.print("");
			return;
		}
		if(con!=null) {
			HttpSession s =  request.getSession();
			s.setAttribute("con", con);
			s.setAttribute("user",user);
			StudentR sr = new StudentR();
			if(sr.isDone(user)) request.setAttribute("havaDone", "haveDone()");
			request.setAttribute("callback", "show()");
			request.getRequestDispatcher("choose.jsp").forward(request, response);
		}
		else {
			request.setAttribute("error", "</br>xxxx登录失败，请重试！");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
